# task2
# 多级菜单
# 三级菜单
# 可进入各级菜单
# 返回主菜单
# 返回上级菜单
# 以省市县做参考


area = {
    '北京': {'朝阳区': ['z1', 'z2'],
           '东城区': ['d1', 'd2'],
           '海淀区': ['h1', 'h2']},
    '上海': {'虹口区': ['k1', 'k2'],
           '浦东新区': ['x1', 'x2'],
           '长宁区': ['n1', 'n2']},
    '重庆': {'渝北区': ['y1', 'y2'],
           '江北区': ['j1', 'j2'],
           '渝中区': ['z1', 'z2']},
}

# menu 1
def display_menu1():
    print('== province menu ==')
    for province in area:
        print(province)

    print('[name]: to city\n[m]: to master\n[q]: exit')


def display_menu2(province):
    print('== city menu ==')
    for city in area.get(province):
        print(city)
    print('[name]: to street\n[up]: to province\n[m]: to master menu\n[q]: exit')

def display_menu3(province,city):
    print('== street menu ==')
    for street in area.get(province).get(city):
        print(street)
    print('[up]: to city\n[m]: to master\n[q]: exit')


display_menu1()
province = ''
city = ''
while True:
    choice = input('choice: ')
    if choice in area:
        province = choice
        display_menu2(province)
    elif choice in area.get(province, []):
        city = choice
        display_menu3(province,city)
    elif choice == 'up':
        if city:
            display_menu2(province)
            city = ''
        elif province:
            display_menu1()
            province = ''
        else:
            print('invalid choice')
    elif choice == 'q':
        print('exit..')
        exit()
    elif choice == 'm':
        province = ''
        city = ''
        display_menu1()
    else:
        print('invalid choice')

